package com.example.myapplication.slice;

import com.example.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.ToastDialog;

public class DialAbilitySlice extends AbilitySlice {
    Text numText;
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_dial);
        //获取数字框
        numText = (Text) findComponentById(ResourceTable.Id_call_number);
        //获取手机上的号码的按钮
        Button btn1  = (Button) findComponentById(ResourceTable.Id_num_1);
        //设置点击的监听方法
        btn1.setClickedListener(this::onClick);
        Button btn2 = (Button) findComponentById(ResourceTable.Id_num_2);
        btn2.setClickedListener(this::onClick);
        Button btn3 = (Button) findComponentById(ResourceTable.Id_num_3);
        btn3.setClickedListener(this::onClick);
        Button btn4 = (Button) findComponentById(ResourceTable.Id_num_4);
        btn4.setClickedListener(this::onClick);
        Button btn5 = (Button) findComponentById(ResourceTable.Id_num_5);
        btn5.setClickedListener(this::onClick);
        Button btn6 = (Button) findComponentById(ResourceTable.Id_num_6);
        btn6.setClickedListener(this::onClick);
        Button btn7 = (Button) findComponentById(ResourceTable.Id_num_7);
        btn7.setClickedListener(this::onClick);
        Button btn8 = (Button) findComponentById(ResourceTable.Id_num_8);
        btn8.setClickedListener(this::onClick);
        Button btn9 = (Button) findComponentById(ResourceTable.Id_num_9);
        btn9.setClickedListener(this::onClick);
        Button btn0 = (Button) findComponentById(ResourceTable.Id_num_0);
        btn0.setClickedListener(this::onClick);
        /* 设置删除数值按钮 */
        Image delImg = (Image) findComponentById(ResourceTable.Id_del);
        delImg.setClickedListener(component -> {
            String text = numText.getText();
            if(text.length()>0){
                text = text.substring(0,text.length()-1);
                numText.setText(text);
            }
        });
    }

    //设置点击后的操作  component 就是被点击的组件对象
    public void onClick(Component component) {
        //强转点击的按钮对象
        Button btn = (Button) component;
        //获取被点击的按钮的文本值
        String num = btn.getText();
        //把输入的按钮的值,追加到手机号码位置
        numText.append(num);
    }
}
